package com.kakaxi.mybilibili.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.internal.NavigationMenuView;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.bean.EventBusObject;
import com.kakaxi.mybilibili.bean.MemberBean;
import com.kakaxi.mybilibili.module.home.MainActivity;
import com.kakaxi.mybilibili.module.user.view.LoginActivity;
import com.kakaxi.mybilibili.utils.ActivityStackManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 侧滑活动
 * 这个活动方便所有活动继承, 让所有活动都能拥有侧滑栏的功能
 * Created by kakaxi on 16/8/10.
 */

public abstract class NavigationActivity extends ProcessActivity {

    /** 侧滑布局用来调用关闭侧滑视图 **/
    private DrawerLayout mDrawerLayout;

    /** 侧滑视图 **/
    private NavigationView mNavigationView;

    private ImageView mBackImg;
    private CircleImageView mFaceImg;
    private TextView mUsernameTxt;

    private boolean mHasLogin = false;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        initViews();
    }

    private void initViews() {
        // 从子类实现的方法中获取活动布局id
        mNavigationView = getNavigationView();
        mDrawerLayout = getDrawerLayout();
        if (mNavigationView != null) {
            // 设置当前所处的菜单分类
            final int currentMenuItemId = getCurrentMenuItemId();
            mNavigationView.setCheckedItem(currentMenuItemId);
            // 设置菜单点击监听器
            mNavigationView.setNavigationItemSelectedListener(new NavigationView
                    .OnNavigationItemSelectedListener() {

                @Override
                public boolean onNavigationItemSelected(MenuItem item) {
                    if (mDrawerLayout != null) {
                        // 点击完关闭侧滑视图
                        mDrawerLayout.closeDrawer(mNavigationView);
                    }
                    // 把主界面上面的活动都清除掉,再进行跳转
                    ActivityStackManager.getInstance().popToActivity(MainActivity.class);
                    switch (item.getItemId()) {
                        case R.id.action_nav_home: {
                            break;
                        }
                        case R.id.action_nav_cache: {
                            break;
                        }
                        case R.id.action_nav_fav: {
                            break;
                        }
                        case R.id.action_nav_history: {
                            break;
                        }
                        case R.id.action_nav_concern: {
                            break;
                        }
                        case R.id.action_nav_money: {
                            break;
                        }
                    }
                    // 在初始化时候已经选中了当前所处的菜单
                    return false;
                }
            });
            // 处理侧滑视图的顶部布局
            ViewGroup headerGroup = (ViewGroup) mNavigationView.getHeaderView(0);
            mFaceImg = (CircleImageView) headerGroup.findViewById(R.id.civ_nav_header_face);
            mUsernameTxt = (TextView) headerGroup.findViewById(R.id.txt_nav_header_nickname);
            mBackImg = (ImageView) headerGroup.findViewById(R.id.img_nav_header_back);
            mBackImg.setAlpha(0.5f);
            mFaceImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mHasLogin) { // 跳转个人信息界面

                    } else { // 跳转登录界面
                        Intent intent = new Intent(NavigationActivity.this, LoginActivity.class);
                        startActivityForResult(intent, LoginActivity.ACT_REQUEST_CODE);
                    }
                }
            });
            // 更新用户信息
            updateNavHeaderUserInfo();
        }
    }

    protected abstract int getCurrentMenuItemId();

    /**
     * 获取侧滑视图
     *
     * @return
     */
    protected NavigationView getNavigationView() {
        if (mNavigationView == null) {
            mNavigationView = (NavigationView) findViewById(R.id.nav_common_slide);
            // 去掉滚动条
            if (mNavigationView != null) {
                NavigationMenuView navigationMenuView = (NavigationMenuView) mNavigationView
                        .getChildAt(0);
                if (navigationMenuView != null) {
                    navigationMenuView.setVerticalScrollBarEnabled(false);
                }
            }
        }
        return mNavigationView;
    }

    /**
     * 获取侧滑布局
     *
     * @return
     */
    protected DrawerLayout getDrawerLayout() {
        if (mDrawerLayout == null) {
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        }
        return mDrawerLayout;
    }


    /**
     * 更新侧滑视图上面的用户信息
     */
    protected void updateNavHeaderUserInfo() {
        MemberBean memberBean = ((MyApplication) getApplication()).getMemberBean();
        if (memberBean != null && memberBean.getMid() != null && !mHasLogin) {
            mHasLogin = true;
            String username = memberBean.getName();
            String face = memberBean.getFace();
            if (username != null && mUsernameTxt != null) mUsernameTxt.setText(username);
            if (face != null && mFaceImg != null) {
                Glide.with(getBaseContext()).load(face).crossFade().into(mFaceImg);
            }
        }
    }

    /**
     * 清除侧滑视图登录的用户信息
     */
    protected void clearNavHeaderUserInfo() {
        mUsernameTxt.setText("点击头像登录");
        mFaceImg.setImageDrawable(ActivityCompat.getDrawable(getBaseContext(), R.drawable
                .bili_default_avatar));
        mHasLogin = false;
    }

    /**
     * 更新header视图的用户信息
     *
     * @param object 事件巴士对象
     */
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onUpdateToolBarEventBus(EventBusObject object) {
        if (object.getEvent() == EventBusObject.EVENT_MAIN_ACT_UPDATE_USER_INFO) {
            updateNavHeaderUserInfo();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case LoginActivity.ACT_REQUEST_CODE: { // 登录界面返回结果
                if (resultCode == RESULT_OK) { // 登录成功
                    updateNavHeaderUserInfo();
                    EventBusObject object = new EventBusObject(EventBusObject
                            .EVENT_MAIN_ACT_UPDATE_USER_INFO, "");
                    EventBus.getDefault().post(object);
                }
                break;
            }
        }
    }
}
